import 'package:flutter/material.dart';

/// 显示SnackBars
/// 1. 创建Scaffold
/// 2. 显示一个SnackBar
/// 3. 提供一个额外的工作

class SnackBarsApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTitle = 'Snack Demo';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: SnackBarsAppPage(title: appTitle),
    );
  }
}

class SnackBarsAppPage extends StatefulWidget {
  final String title;

  SnackBarsAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => SnackBarsAppPageState();
}

class SnackBarsAppPageState extends State<SnackBarsAppPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SnackBarPage(),
    );
  }
}

class SnackBarPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        onPressed: () {
          // 创建SnackBar
          final snackBar = SnackBar(
            content: Text('Yay! A SnackBar!'),
            action: SnackBarAction(
                label: 'Undo',
                onPressed: () {
                  print('撤销');
                }),
          );
          // 显示SnackBar
          Scaffold.of(context).showSnackBar(snackBar);
        },
        child: Text('Show SnackBar'),
      ),
    );
  }
}
